package com.zhumo.sz.gaode;

import com.alibaba.fastjson.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 商圈实体
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Business {

    private String name;

    private Double lng;

    private Double lat;

    //商圈id存在重复 不启用
//    private String id;

    public static Business build(JSONObject busJson){
        String location = busJson.getString("location");
        Double lat = Double.valueOf(location.split(",")[1]);
        Double lng = Double.valueOf(location.split(",")[0]);
        return new Business(busJson.getString("name"),lng,lat);
    }

    /**
     * 转换格式如入Mysql
     */
    public static JSONObject parseBusForMysql(String arrayStr){
        try {
            JSONObject result = new JSONObject();
            //判断返回结果是否是json
            if (JSONValidator.from(arrayStr).validate()){
                //返回结果是json
                return new JSONObject();
            }
            JSONArray jsonArray = JSONArray.parseArray(arrayStr);
            jsonArray.forEach( json -> {
                JSONObject son = JSONObject.parseObject(JSON.toJSONString(json));
                JSONObject buildSon = (JSONObject) JSON.toJSON(Road.build(son));
                String name = buildSon.getString("name");
                result.put(name,buildSon);
            });
            return result;
        }catch (JSONException e){
            e.printStackTrace();
            return new JSONObject();
        }
    }
}
